/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.backends.jena;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 *
 * @author Laurent WOUTERS
 */
public abstract class JenaCustomBuiltin extends com.hp.hpl.jena.reasoner.rulesys.builtins.BaseBuiltin {
    private static final String genBuiltinPrefix = "http://www.xowl.org/interpreter/builtins#";
    private static Integer genBuiltinCount = 0;

    protected JenaRepository repository;
    protected Map<xowl.lang.actions.QueryVariable, com.hp.hpl.jena.graph.Node> variables;
    protected String name;
    
    public JenaCustomBuiltin(JenaRepository repo, Map<xowl.interpreter.backends.rdf.XOWLVariableNode, com.hp.hpl.jena.graph.Node> vars) {
        this.repository = repo;
        this.variables = new HashMap<xowl.lang.actions.QueryVariable, com.hp.hpl.jena.graph.Node>();
        for (Entry<xowl.interpreter.backends.rdf.XOWLVariableNode, com.hp.hpl.jena.graph.Node> entry : vars.entrySet())
            variables.put(entry.getKey().getVariable(), entry.getValue());
        name = "anonymous" + genBuiltinCount.toString();
        genBuiltinCount++;
        com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry.theRegistry.register(this);
    }

    @Override public int getArgLength() { return variables.size(); }
    @Override public String getName() { return name; }
    @Override public String getURI() { return genBuiltinPrefix + name; }
    @Override public boolean isSafe() { return true; }
    @Override public boolean isMonotonic() { return true; }

    protected xowl.interpreter.impl.AggregatedEvaluator getEvaluator() { return repository.getDefaultEvaluator(); }
    
    protected JenaQueryVariableEvaluator getQueryEvaluator(com.hp.hpl.jena.reasoner.rulesys.RuleContext rc) {
        return new JenaQueryVariableEvaluator(repository, variables, rc);
    }
}
